<?php

class MainController extends Controller
{
    // path to user workspace layou
    //public $layout="application.views.layouts.mainl";
    
	public function actionIndex()
	{
		  $id_proj=$_GET['id'];
		$user = User::model()->findByPk(Yii::app()->user->id);
        $task= Task::model()->recently()->findAll("user_id=:user_id AND project_id=:project_id",array(':user_id'=>Yii::app()->user->id,':project_id'=>$id_proj));
        $input="www";
        $output="qwe";


        // если запрос не асинхронный, отдаём форму полностью
        
        $this->render('index',array('user'=>$user, 'projects'=>$task,'input'=>$input,
        'output'=>$output,'id_proj'=>$id_proj));
	}

public function actionAdd()
    {

            
        if(isset($_POST['input_name']))
        {
            $input_name = $_POST['input_name'];
            $input_about = $_POST['input_about'];
       
        }
        if(Yii::app()->request->isAjaxRequest)
        {
            $id_proj=$_POST['id'];
          
            //$id_proj = $_POST['id_project'];
            $Task= new Task();

            $Task->task_name=$input_name;
           $Task->text=$input_about;
            $Task->user_id=Yii::app()->user->id;
            $Task->project_id=$id_proj;
            $Task->date=date('Y-m-d');
                $task_find= Task::model()->findAll("user_id=:user_id AND project_id=:project_id",array(':user_id'=>Yii::app()->user->id,':project_id'=>$id_proj));
            if($Task->save())
            {
                //Расчет максимального занчения Номера таска
                $task_find = Task::model()->findAll("user_id=:user_id AND project_id=:project_id",array(':user_id'=>Yii::app()->user->id,':project_id'=>$id_proj));
                $count=Task::model()->count("user_id=:user_id AND project_id=:project_id",array(':user_id'=>Yii::app()->user->id,':project_id'=>$id_proj));
                $max=$task_find[0]['tesknum'];
                for ($i = 1; $i < $count; $i++) {
                    if($task_find[$i]['tesknum']>$max)
                    {
                        $max=$task_find[$i]['tesknum'];
                    }
                }
                $Task->tesknum=$max+1;

                $Task->save();
            }
            
            //Составление обновленного списка проэктов
            $Task = Task::model()->recently()->findAll("user_id=:user_id AND project_id=:project_id",array(':user_id'=>Yii::app()->user->id,':project_id'=>$id_proj));
            $input_name='';
            //print_r($Task);
            foreach ($Task  as $key)
            {
                $input_name.= "<li class='recArray_".$key->tesknum."'>".$key->task_name.'</li>';
            }
        //Вывод на экран обновленного списка проэктов
            echo $input_name;

        }

    }
    
    public function actionSortAndSave()
    {
    	$id_proj=$_POST['id'];
    	
         $task = Task::model()->recently()->findAll("user_id=:user_id AND project_id=:project_id",array(':user_id'=>Yii::app()->user->id,':project_id'=>$id_proj));
    	
    	      
        $updateRecordArray=$_POST['recArray'];
        
		
        
        $i=0;
        
        foreach ($task as $key)
        {
            
          $task[$updateRecordArray[$i]-1]['tesknum']=$i+1;
            $task[$updateRecordArray[$i]-1]->save();
            $i++;
        }
            
         $task = Task::model()->recently()->findAll("user_id=:user_id AND project_id=:project_id",array(':user_id'=>Yii::app()->user->id,':project_id'=>$id_proj));
                    
            foreach ($task as $key) 
                {
                    
                    echo "<li id='recArray_".$key->tesknum."'>".$key->task_name.'</li>';
                }
    
        
    }

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}